<?php
namespace User\Controller;

use think\Controller;

class MyDealController extends  Controller{

    //我的交易历史
    public function myDeal_history(){
        $user = session('user');
        if(empty($user)){echo "账号信息过期,请重新登录";exit;}

        $fo = I('fo')? I('fo'):'';
        $to = I('to')? I('to'):'';
        $type = I('type')?I('type'):'';
        $where = '';
        if(!empty($fo) && !empty($to)){
            $where['CLOSE_TIME'] = array('between',array($fo,$to.' 23:59:59'));
        }
        if($type==1){
            $where['CMD'] = 1;
        }else if($type==2){
            $where['CMD'] = 2;
        }
        if(empty($where)){
            $this->assign('user',$user);
            $this->display();
        }else{
            if($where['CMD']==1){
                $where['CMD'] = 0;
            }elseif($where['CMD']==2){
                $where['CMD'] = 1;
            }else{
                $where['CMD']=array('lt',2);
            }
            $where['LOGIN']   = $user['user_id'];

            $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//            if($user['type']==1){
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//            }else{
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//            }
            $count =  $mt4_trades->where($where)->count();
            $Page = new \Think\Page($count, 20);
            $Page->parameter['fo']= $fo;
            $Page->parameter['to']= $to;
            $show = $Page->show();// 分页显示输出
            $data = $mt4_trades->where($where)->order('TICKET desc')
                ->field('LOGIN,TICKET,SYMBOL,CMD,VOLUME,OPEN_TIME,OPEN_PRICE,CLOSE_TIME,CLOSE_PRICE,SL,TP,COMMISSION,SWAPS,PROFIT,COMMENT')
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();

            foreach ($data as $k=>$v){
                if($v['cmd']==0){                                       //交易类型
                    $data[$k]['cmd'] = 'BUY';
                }else if($v['cmd']==1){
                    $data[$k]['cmd'] = 'SELL';
                }
                if(strtotime($v['close_time'])==-10800){                //关仓时间
                    $data[$k]['close_time']  = '--  -- --';
                }
                $data[$k]['volume']      = $v['volume']/100;          //交易量
            }
            $sum_num = $mt4_trades->field('sum(VOLUME),sum(COMMISSION),sum(SWAPS),sum(PROFIT)')->where($where)->select();
            $this->assign('type',$type);
            $this->assign('data',$data);
            $this->assign('num',$sum_num);
            $this->assign('to',$to);
            $this->assign('fo',$fo);
            $this->assign('user',$user);
            $this->assign('page',$show);
            $this->display();
        }
    }
    //下载我的交易历史
     public function myDeal_history_download(){
         $user = session('user');
         if(empty($user)){echo "账号信息过期,请重新登录";exit;}
         $fo = I('fo')? I('fo'):'';
         $to = I('to')? I('to').' 23:59:59':'';
         $type = I('type')?I('type'):'';
         $where = '';
         if(($fo=='' || $to=='') && $type==''){
             echo "请查询后在下载";die;
         }
         if(!empty($fo) && !empty($to)){
             $where['CLOSE_TIME'] = array('between',array($fo,$to));
         }
         if(empty($type)){
             $where['CMD'] = array('lt',2);
         }elseif($type==1){
             $where['CMD'] = 0;
         }else{
             $where['CMD'] = 1;
         }
         $where['LOGIN']   = $user['user_id'];
         //$where['SYMBOL']  = array('like','%bo%');
         $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//         if($user['type']==1){
//             $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//         }else{
//             $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//         }
         $data = $mt4_trades->where($where)->order('TICKET desc')->field('LOGIN,TICKET,SYMBOL,CMD,VOLUME,OPEN_TIME,OPEN_PRICE,CLOSE_TIME,CLOSE_PRICE,SL,TP,COMMISSION,SWAPS,PROFIT,COMMENT')->select();

         $data_csv= [];
         foreach ($data as $k=>$v){
             $data_csv[$k]['ticket']     = $v['ticket'];                  //订单号
             $data_csv[$k]['login']      = $v['login'];                   //mt4账号
             $data_csv[$k]['symbol']     = $v['symbol'];                  //交易品种
             if($v['cmd']==0){                                            //交易类型
                 $data_csv[$k]['cmd'] = 'BUY';
             }else if($v['cmd']==1){
                 $data_csv[$k]['cmd'] = 'SELL';
             }
             $data_csv[$k]['volume']      =  round($v['volume']/100,2);          //交易量
             $data_csv[$k]['open_time']   = $v['open_time'];           //开仓时间
             $data_csv[$k]['open_price']   = $v['open_price'];         //开仓价格
             $data_csv[$k]['sl'] = $v['sl'];
             $data_csv[$k]['tp'] = $v['tp'];
             $data_csv[$k]['close_time']  = $v['close_time'];            //关仓时间
             $data_csv[$k]['close_price']  = $v['close_price'];         //关仓价格
             $data_csv[$k]['commission'] = $v['commission'];            //手续费
             $data_csv[$k]['swaps'] =  $v['swaps'];                     //库存费
             $data_csv[$k]['profit'] = round($v['profit'],2); //获利
             $data_csv[$k]['comment'] = $v['comment'];                  //注释
         }
         $csv=new \Think\Csv();
         $csv_title=array('订单号','MT4','交易品种','交易类型','交易量','开仓时间','开仓价格','SL','TP','关仓时间','关仓价格','手续费','库存费','收益','注释');
         $csv->put_csv($data_csv,$csv_title,"交易历史");

    }
    //在途订单
    public function Ontheway_order(){
        $user = session('user');
        if(empty($user)){echo "账号信息过期,请重新登录";exit;}
        $fo = I('fo')? I('fo'):'';
        $to = I('to')? I('to'):'';
        $type = I('type')?I('type'):'';
        $where = '';
        if(!empty($fo) && !empty($to)){
            $where['OPEN_TIME'] = array('between',array($fo,$to.' 23:59:59'));
        }
        if($type==1){
            $where['CMD'] = 1;
        }else if($type==2){
            $where['CMD'] = 2;
        }
        if(empty($where)){
            $this->assign('user',$user);
            $this->display();
        }else{
            $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//            if($user['type']==1){       //1-数字 2-外汇
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//            }else{
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//            }

            if($where['CMD']==1){
                $where['CMD'] = 0;
            }elseif($where['CMD']==2){
                $where['CMD'] = 1;
            }else{
                $where['CMD']=array('lt',2);
            }
            $where['LOGIN']= $user['user_id'];
            $where['CLOSE_TIME'] = '1970-01-01 00:00:00';
            //$where['SYMBOL']  = array('like','%bo%');
            $count =  $mt4_trades->where($where)->count();
            $Page = new \Think\Page($count, 20);
            $Page->parameter['fo']= $fo;
            $Page->parameter['to']= $to;
            $show = $Page->show();// 分页显示输出
            $data = $mt4_trades->where($where)->order('TICKET desc')->field('LOGIN,TICKET,SYMBOL,CMD,VOLUME,OPEN_TIME,OPEN_PRICE,CLOSE_TIME,CLOSE_PRICE,SL,TP,COMMISSION,SWAPS,PROFIT,COMMENT')->limit($Page->firstRow . ',' . $Page->listRows)->select();
            foreach ($data as $k=>$v){
                if($v['cmd']==0){                                       //交易类型
                    $data[$k]['cmd'] = 'BUY';
                }else if($v['cmd']==1){
                    $data[$k]['cmd'] = 'SELL';
                }
                $data[$k]['volume']      = $v['volume']/100;          //交易量
                if(strtotime($v['close_time'])==-10800){             //关仓时间
                    $data[$k]['close_time']  = '--  -- --';
                }
                $data_csv[$k]['profit'] = round($v['profit'],2); //获利
            }
            $this->assign('type',$type);
            $this->assign('data',$data);
            $this->assign('to',$to);
            $this->assign('fo',$fo);
            $this->assign('user',$user);
            $this->assign('page',$show);
            $this->display();
        }
    }
    //下载在途订单
    public function Ontheway_order_download(){
        $user = session('user');
        if(empty($user)){echo "账号信息过期,请重新登录";exit;}
        $fo = I('fo')? I('fo'):'';
        $to = I('to')? I('to'):'';
        $type = I('type')?I('type'):'';
        if(($fo=='' || $to=='') && $type==''){
            echo "请查询后在下载";die;
        }
        $where = '';
        if(!empty($fo) && !empty($to)){
            $where['OPEN_TIME'] = array('between',array($fo,$to.' 23:59:59'));
        }
        if(empty($type)){
            $where['CMD'] = array('lt',2);
        } elseif ($type==1) {
            $where['CMD'] = 0;
        } else {
            $where['CMD'] = 1;
        }

        $where['LOGIN']      = $user['user_id'];
        $where['CLOSE_TIME'] = '1970-01-01 00:00:00';
        //$where['SYMBOL']     = array('like','%bo%');
        $mt4_trades = M(' mt4_trades','','DB_CONFIG2');
//            if($user['type']==1){       //1-数字 2-外汇
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//            }else{
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//            }
        $data = $mt4_trades->where($where)->order('TICKET desc')->field('LOGIN,TICKET,SYMBOL,CMD,VOLUME,OPEN_TIME,OPEN_PRICE,CLOSE_TIME,CLOSE_PRICE,SL,TP,COMMISSION,SWAPS,PROFIT,COMMENT')->select();
        foreach ($data as $k=>$v){
            $data_csv[$k]['ticket']     = $v['ticket'];                 //订单号
            $data_csv[$k]['login']      = $v['login'];                  //mt4账号
            $data_csv[$k]['symbol']     = $v['symbol'];                 //交易品种
            if($v['cmd']==0){                                           //交易类型
                $data_csv[$k]['cmd'] = 'BUY';
            }else if($v['cmd']==1){
                $data_csv[$k]['cmd'] = 'SELL';
            }
            $data_csv[$k]['volume']      =  round($v['volume']/100,2);//交易量
            $data_csv[$k]['open_time']   = $v['open_time'];           //开仓时间
            $data_csv[$k]['open_price']   = $v['open_price'];         //开仓价格
            $data_csv[$k]['sl'] = $v['sl'];
            $data_csv[$k]['tp'] = $v['tp'];
            if(strtotime($v['close_time'])==-10800){                   //关仓时间
                $data_csv[$k]['close_time']  = '--  -- --';
            }
            $data_csv[$k]['close_price']  = $v['close_price'];          //关仓价格
            $data_csv[$k]['commission'] = $v['commission'];             //手续费
            $data_csv[$k]['swaps'] = $v['swaps'];                       //库存费
            $data_csv[$k]['profit'] =round($v['profit'],2);   //获利
            $data_csv[$k]['comment'] = $v['comment'];                    //注释
        }
        $csv=new \Think\Csv();
        $csv_title=array('订单号','MT4','交易品种','交易类型','交易量','开仓时间','开仓价格','SL','TP','关仓时间','关仓价格','手续费','库存费','收益','注释');
        $csv->put_csv($data_csv,$csv_title,"在途订单");
    }
    //限价订单
    public function fixed_order(){
        $user =session('user');
        if(empty($user)){echo "账号信息过期,请重新登录";exit;}
        $fo = I('fo')? I('fo'):'';
        $to = I('to')? I('to'):'';
        $type = I('type')?I('type'):'';
        $where = '';

        if(!empty($fo) && !empty($to)){
            $where['OPEN_TIME'] = array('between',array($fo,$to.' 23:59:59'));
        }
        if(!empty($type)){
            $where['CMD'] = $type;
        }
        if(empty($where)){
            $this->assign('user',$user);
            $this->display();
        }else{
            if(empty($where['CMD'])){
                $where['CMD']=array('between', array(2, 3));
            }
            $where['LOGIN']= $user['user_id'];
            $mt4_trades = M(' mt4_trades','','DB_CONFIG2');
//            if($user['type']==1){       //1-数字 2-外汇
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//            }else{
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//            }
            $count =  $mt4_trades->where($where)->count();
            $Page = new \Think\Page($count, 20);
            $Page->parameter['fo']= $fo;
            $Page->parameter['to']= $to;
            $show = $Page->show();// 分页显示输出
            $data = $mt4_trades->where($where)->order('TICKET desc')
                ->field('LOGIN,TICKET,SYMBOL,CMD,VOLUME,OPEN_TIME,OPEN_PRICE,CLOSE_TIME,CLOSE_PRICE,SL,TP,COMMISSION,SWAPS,PROFIT,COMMENT')
                ->limit($Page->firstRow . ',' . $Page->listRows)->select();

            foreach ($data as $k=>$v){
                if ($v['cmd'] ==2) {                                    //交易类型
                    $data[$k]['cmd'] = 'BUY_LIMIT';
                }elseif ($v['cmd'] == 3){
                    $data[$k]['cmd'] = 'SELL_LIMIT';
                }
                $data_csv[$k]['volume']      =  round($v['volume']/100,2);//交易量
                if(strtotime($v['close_time'])==-10800){              //关仓时间
                    $data[$k]['close_time']  = '--  -- --';
                }
                $data_csv[$k]['profit'] =round($v['profit'],2);   //获利
            }

            $this->assign('type',$type);
            $this->assign('data',$data);
            $this->assign('to',$to);
            $this->assign('fo',$fo);
            $this->assign('user',$user);
            $this->assign('page',$show);
            $this->display();
        }
    }
    //下载限价订单
    public function fixed_order_download(){
        $user = session('user');
        if(empty($user)){echo "账号信息过期,请重新登录";exit;}
        $fo = I('fo')? I('fo'):'';
        $to = I('to')? I('to'):'';
        $type = I('type')?I('type'):'';
        $where = '';
        if(!empty($fo) && !empty($to)){
            $where['OPEN_TIME'] = array('between',array($fo,$to.' 23:59:59'));
        }else{
            echo "请先查询要下载的数据";die;
        }
        if(empty($type)){
            $where['CMD'] = array('between', array(2, 3));;
        }else{
            $where['CMD'] = $type;
        }
        $where['LOGIN']= $user['user_id'];
        $mt4_trades = M(' mt4_trades','','DB_CONFIG2');
//            if($user['type']==1){       //1-数字 2-外汇
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//            }else{
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//            }
        $data = $mt4_trades->where($where)->order('TICKET desc')->field('LOGIN,TICKET,SYMBOL,CMD,VOLUME,OPEN_TIME,OPEN_PRICE,CLOSE_TIME,CLOSE_PRICE,SL,TP,COMMISSION,SWAPS,PROFIT,COMMENT')->select();
        foreach ($data as $k=>$v){
            $data_csv[$k]['ticket']     = $v['ticket'];                 //订单号
            $data_csv[$k]['login']      = $v['login'];                  //mt4账号
            $data_csv[$k]['symbol']     = $v['symbol'];                 //交易品种
            if ($v['cmd'] ==2) {                                        //交易类型
                $data[$k]['cmd'] = 'BUY_LIMIT';
            }elseif ($v['cmd'] == 3){
                $data[$k]['cmd'] = 'SELL_LIMIT';
            }
            $data_csv[$k]['volume']      =  round($v['volume']/100,2);//交易量
            $data_csv[$k]['open_time']   = $v['open_time'];           //开仓时间
            $data_csv[$k]['open_price']   = $v['open_price'];         //开仓价格
            $data_csv[$k]['sl'] = $v['sl'];
            $data_csv[$k]['tp'] = $v['tp'];
            if(strtotime($v['close_time'])==-10800){                //关仓时间
                $data_csv[$k]['close_time']  = '--  -- --';
            }
            $data_csv[$k]['close_price']  = $v['close_price'];       //关仓价格
            $data_csv[$k]['commission'] = $v['commission'];          //手续费
            $data_csv[$k]['swaps'] = $v['swaps'];                    //库存费
            $data_csv[$k]['profit'] =round($v['profit'],2); //获利
            $data_csv[$k]['comment'] = $v['comment'];                //注释
        }

        $csv=new \Think\Csv();
        $csv_title=array('订单号','MT4','交易品种','交易类型','交易量','开仓时间','开仓价格','SL','TP','关仓时间','关仓价格','手续费','库存费','获利','注释');
        $csv->put_csv($data_csv,$csv_title,"限价订单");
    }
    //来往资金
    public function Funding(){
        $user = session('user');
        if(empty($user)){echo "账号信息过期,请重新登录";exit;}
        $fo = I('fo') ? I('fo'):'';
        $to = I('to') ? I('to'): '';
        $where = '';
        if (!empty($fo) && !empty($to)) {
            $where['OPEN_TIME'] = array('between', array($fo, $to.' 23:59:59'));
        }
        if(empty($where)){
            $this->assign('user',$user);
            $this->display();
        }else {
            $where['LOGIN'] = $user['user_id'];
            $where['CMD'] = 6;
            $mt4_trades = M(' mt4_trades', '', 'DB_CONFIG2');
//            if($user['type']==1){       //1-数字 2-外汇
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//            }else{
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//            }
            $count = $mt4_trades->where($where)->count();
            $Page = new \Think\Page($count, 20);
            $Page->parameter['fo'] = $fo;
            $Page->parameter['to'] = $to;
            $show = $Page->show();// 分页显示输出
            $data = $mt4_trades->where($where)->order('TICKET desc')
                ->field('TICKET,LOGIN,CMD,SL,TP,COMMISSION,PROFIT,COMMENT')
                ->limit($Page->firstRow . ',' . $Page->listRows)->select();

            $this->assign('data', $data);
            $this->assign('to', $to);
            $this->assign('fo', $fo);
            $this->assign('user', $user);
            $this->assign('page', $show);
            $this->display();
        }
    }
    //下载来往资金
    public function Funding_download(){
        $user = session('user');
        if(empty($user)){echo "账号信息过期,请重新登录";exit;}
        $fo = I('fo') ? I('fo'): '';
        $to = I('to') ? I('to'): '';
        $where = '';
        if (empty($fo) || empty($to)) {
            echo "请查询数据后下载";exit;
        } else {
            $where['OPEN_TIME'] = array('between', array($fo,$to.' 23:59:59'));
        }
        $where['LOGIN'] = $user['user_id'];
        $where['CMD'] = 6;
        $mt4_trades = M(' mt4_trades', '', 'DB_CONFIG2');
//            if($user['type']==1){       //1-数字 2-外汇
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//            }else{
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//            }
        $data = $mt4_trades->where($where)->order('TICKET desc')
            ->field('TICKET,LOGIN,CMD,SL,TP,COMMISSION,PROFIT,COMMENT')->select();
        foreach ($data as $k=>$v){
            $data_csv[$k]['ticket']     = $v['ticket'];                 //订单号
            $data_csv[$k]['login']      = $v['login'];                  //mt4账号
            if ($v['cmd'] ==6) {                                        //交易类型
                $data[$k]['cmd'] = 'BALANCE';
            }
            $data_csv[$k]['sl'] = $v['sl'];
            $data_csv[$k]['tp'] = $v['tp'];
            $data_csv[$k]['profit'] =round($v['profit'],2); //获利
            $data_csv[$k]['comment'] = $v['comment'];                //注释
        }
        $csv=new \Think\Csv();
        $csv_title=array('订单号','MT4','SL','TP','获利','注释');
        $csv->put_csv($data_csv,$csv_title,"来往资金");
    }

    public function profitLog(){//余额宝收益
        $user = session('user');
        if(empty($user)){echo "账号信息过期,请重新登录";exit;}
        $from=I('from') ? I('from') : 0;
        $to=I('to') ? I('to') : 0;
        $status=I('status') ? I('status') : 0;
        $where='(user_id = '.$user['user_id'].') ';
        if(!empty($status)){
            if($status==1){
                if(empty($where)){
                    $where.=' (status = 1)';
                }else{
                    $where.=' and (status = 1)';
                }
            }else{
                if(empty($where)){
                    $where.=' (status <> 1)';
                }else{
                    $where.=' and (status <> 1)';
                }
            }
        }
        if(!empty($from)&&!empty($to)){
            if(empty($where)){
                $where.=' (`date` between \''.$from.'\' and \''.$to.'\') ';
            }else{
                $where.=' and (`date` between \''.$from.'\' and \''.$to.'\') ';
            }
            $this->assign('from',$from);
            $this->assign('to',$to);
        }
        $model=M('profit');
        if(empty($where)){
            $count=$model->count();
        }else{
            $count=$model->where($where)->count();
        }
        $Page = new \Think\Page($count,25);
        $Page->parameter['status']=$status;
        $Page->parameter['from']=$from;
        $Page->parameter['to']=$to;
        $show = $Page->show();
        if(empty($where)){
            $list=$model->field('id,user_id,balance,profit,rate,date,time,status')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        }else{
            $list=$model->field('id,user_id,balance,profit,rate,date,time,status')->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        }
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->assign('status',$status);
        $this->display();
    }

}